package com.ellecity06.cwhiskymall.ui.user

import com.ellecity06.common.base.BaseMviViewModel
import com.ellecity06.common.ktx.aesDecode
import com.ellecity06.common.ktx.postEvent
import com.ellecity06.common.utils.ImageUploadUtils
import com.ellecity06.cwhiskymall.bean.request.UpdateUserRequest
import com.ellecity06.cwhiskymall.constant.Constants
import com.ellecity06.cwhiskymall.http.request

/**
 * @author ellecity06
 * @time 2024/10/26 11:53
 * @des
 */
class UserInfoViewModel : BaseMviViewModel<UserInfoState, UserInfoIntent>() {
    private var imagePath: String? = null
    private var name :String ?=null
    private var imageUrl: String? = null
    override suspend fun collectIntent(it: UserInfoIntent) {
        when (it) {
            is UserInfoIntent.User -> {
                fetchUser()
            }
            is UserInfoIntent.ImageSelect -> {
                this.imagePath = it.path
            }
            is UserInfoIntent.EditUser -> {
                editUser(it.name)
            }
            is UserInfoIntent.DestroyAccount -> {
                destroyAccount()
            }

            is UserInfoIntent.Back -> {
                if (imagePath != null) {
                    mState.postEvent(UserInfoState.ShowConfirm)
                    return
                }
                if (it.name!=name){
                    mState.postEvent(UserInfoState.ShowConfirm)
                    return
                }
                mState.postEvent(UserInfoState.Back)
            }
            is UserInfoIntent.Logout -> {
                logout()
            }
            else -> {}
        }
    }
    private fun logout() {
        request(api = { logout() }, showLoading = true, success = {
            mState.postEvent(UserInfoState.LogoutSuccess)

        })
    }
    private fun destroyAccount() {
        request(api = {cancelMember()}, showLoading = true, success = {
            mState.postEvent(UserInfoState.DestroySuccess)
        })
    }

    private fun editUser(name: String? = null) {
        showLoading()
        if (imagePath != null) {
            // 选择了图片
            request(api = { ossSts() }, success = {
                ImageUploadUtils.upload(
                    it?.endpoint ?: "",
                    it?.bucket ?: "",
                    (it?.AccessKeyId ?: "").aesDecode(Constants.AES_KEY),
                    it?.AccessKeySecret ?: "",
                    it?.SecurityToken ?: "",
                    imagePath!!,
                    onFail = {
                        dismissLoading()
                    }
                ) {imageUrl->
                    val request = UpdateUserRequest(imageUrl, name)
                    request(api = { updateUser(request) }, showLoading = true, success = {
                        mState.postEvent(UserInfoState.UpdateSuccess)
                    })// 上传成功了

                }
            })


        } else {
            val request = UpdateUserRequest(nickName = name)
            request(api = { updateUser(request) }, showLoading = true, success = {
                mState.postEvent(UserInfoState.UpdateSuccess)
            })
        }

    }

    private fun fetchUser() {
        request(api = { userInfo() }, success = {
            if (it != null){
                name = it.nickName
                mState.postEvent(UserInfoState.UserInfo(it))

            }
        })
    }
}